package com.attendance.sz;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.attendance.sz.local.DBConstants;
import com.attendance.sz.local.LocalCardRecordDao;
import com.attendance.sz.utils.ClsPublic;
import com.attendance.sz.utils.DataCleanManager;
import com.attendance.sz.utils.DialogResult;
import com.attendance.sz.utils.MyApplication;
import com.attendance.sz.utils.MyModelDialog;
import com.attendance.sz.utils.SelfDefineLoadingDialog;
import com.attendance.sz.utils.Session;
import com.attendance.sz.utils.Version;
import com.attendance.sz.utils.logMa;
import com.bumptech.glide.Glide;
import com.tencent.bugly.crashreport.CrashReport;
import com.yc.pos.Record;
import com.yc.utilslib.utils.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;

public class ManagerActivity extends BaseNewActivity {
    private static final String LOGTAG = "martrin";
    public static ManagerActivity activityOne;
    LocalCardRecordDao localCardRecordDao = null;
    private ArrayList<Record> mRecordList = new ArrayList<>();
    TextView tvTitle;
    RadioButton rbFirst, rbSecond, rbThree;
    LinearLayout llHeaderLeft, llContent;
    Context myContext;
    Activity myActivity;

    LinearLayout ll_onepwd, ll_secondpwd;
    LinearLayout ll_reboottime;
    LinearLayout ll_residencetime;//界面停留时间
    LinearLayout ll_advertdownurl;//广告下载播放
    LinearLayout ll_monitor;
    LinearLayout ll_ftpserver;//ftp server
    LinearLayout ll_telnetd;//telnetd server
    LinearLayout ll_serverlogip;//日志IP
    LinearLayout ll_cardtype; //卡类型
    LinearLayout ll_synchronizationtime;//校对时间相差

    LayoutInflater inflater;
    WindowManager manager;

    LinearLayout ll_machineno, ll_deviceno, ll_intervaltime, ll_overtime, ll_cache,
            ll_serverip, ll_localip, ll_appdownurl, ll_companyinfo, ll_appdowntiem, ll_com,ll_key,ll_section,ll_appPhoto;
    TextView tv_machineno, tv_intervaltime, tv_overtime, tv_deviceno,
            tv_versionno, tv_companyinfo, tv_localip, tv_localnetmask,
            tv_localgetway, tv_serverip, tv_serverport, tv_url,tv_key;

    TextView tv_ReBoottime;
    TextView tv_residencetime;//停留时间
    TextView tv_adverturl;//广告
    TextView tv_serverlogip;//日志服务器的IP
    TextView tv_serverlogport;//日志服务器的端口
    TextView tv_cardtype;//卡类型
    TextView tv_synchronizationtime;//校对时间相差
    TextView tv_monitor;
    TextView tv_ftpserver;
    TextView tv_telnetd;
    TextView tv_cache;
    TextView tv_comp;
    TextView tv_section,    tv_photo;

    WifiManager wifiManager;
    LinearLayout ly_version,ly_setBtn;
    RadioGroup radioGroup,radioCameraGroup;
    RadioButton radioYes,radioNo,radioCameraEnable, radioCameraDisable;
    Button btnRecord,btnSysSet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //谢谢了，我也遇到这种问题，发现确实是在Manifest里面设置了没有标题栏，换了一种设置方法就解决了。
        //androidMenifest.xml把application的主题换掉即可
        setTheme(android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen);

        setContentView(R.layout.activity_manager);
        localCardRecordDao = new LocalCardRecordDao();
        if (0 == ClsPublic.imyDeivceID) { ////0--表示我的手机ANDROID设备

        } else {
            //disp bar

        }


        activityOne = this;

        myContext = this;
        myActivity = this;
        inflater = LayoutInflater.from(this);
        manager = this.getWindowManager();
        btnSysSet = (Button) findViewById(R.id.btnSysSet);
        btnSysSet.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
            }
        });
        btnRecord= (Button) findViewById(R.id.btnRecord);
        btnRecord.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                try {
                    loadRecordMessage();
                    Toast.makeText(myContext, "读取本地记录--共"+ mRecordList.size() +"条", Toast.LENGTH_LONG).show();
                    /*for(int i=0;i<10;i++){
                        Record record = new Record();
                        record.id = "21001"+i;
                        record.recordNo = "21112"+i;
                        record.time = "20180302"+i;
                        record.address = "109";
                        mRecordList.add(record);
                    }*/
                    Record record1 = new Record();
                    record1.id = "ID";
                    record1.recordNo = "Line_NO";
                    record1.time = "Create_Time";
                    record1.address = "Device_NO";
                    mRecordList.add(0,record1);

                    String filePath = "/mnt/sdcard/Attendance/" + "filed" + "/";
                    File temp = new File(filePath);
                    if (!temp.exists()) {
                        temp.mkdirs();
                    }
                    int size = mRecordList.size();

                    FileWriter fw = new FileWriter(filePath + "/Record.txt");//SD卡中的路径
                    fw.flush();
                    for (int i=0;i<size;i++){
                        Record mRecord=mRecordList.get(i);
                        fw.write(mRecord.id+","+ mRecord.recordNo+","+mRecord.time+","+mRecord.address+"\r\n");
                    }

                    fw.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        //wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifiManager = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);

        tvTitle = (TextView) findViewById(R.id.tvTitle);

        llHeaderLeft = (LinearLayout) findViewById(R.id.llHeaderLeft);
        llHeaderLeft.setOnClickListener(clickListener);
        tv_photo=(TextView)findViewById(R.id.    tv_photo);
        tv_photo.setText(Session.getDownPhotoUrl());
        rbFirst = (RadioButton) findViewById(R.id.rbFirst);
        rbFirst.setOnClickListener(clickListener);
        rbSecond = (RadioButton) findViewById(R.id.rbSecond);
        rbSecond.setOnClickListener(clickListener);
        ll_com = (LinearLayout) findViewById(R.id.ll_com);
        ll_appPhoto=(LinearLayout)findViewById(R.id.ll_appPhoto);

        ll_com.setOnClickListener(clickListener);
        ll_section=(LinearLayout)findViewById(R.id.ll_section);
        rbThree = (RadioButton) findViewById(R.id.rbThree);
        rbThree.setOnClickListener(clickListener);
        ll_section.setOnClickListener(clickListener);
        llContent = (LinearLayout) findViewById(R.id.llContent);
        ll_cardtype = (LinearLayout) findViewById(R.id.ll_cardtype); //卡类型
        ll_cardtype.setOnClickListener(clickListener);
        tv_cardtype = (TextView) findViewById(R.id.tv_cardtype);

        ly_version = (LinearLayout) findViewById(R.id.ly_version);
        ly_setBtn = (LinearLayout) findViewById(R.id.ly_setBtn);
        ly_version.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ly_setBtn.setVisibility(View.VISIBLE);
            }
        });

        radioYes = (RadioButton) findViewById(R.id.radioYes);
        radioNo = (RadioButton) findViewById(R.id.radioNo);
        if("1".equals(Session.getVisibility())){
            radioYes.setChecked(true);
        }else{
            radioNo.setChecked(true);
        }
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                // TODO Auto-generated method stub
                            //获取变更后的选中项的ID
                                int radioButtonId = radioGroup.getCheckedRadioButtonId();
                            //根据ID获取RadioButton的实例
                                RadioButton rb = (RadioButton)ManagerActivity.this.findViewById(radioButtonId);
                                //更新文本内容，以符合选中项
                                 if("是".equals(rb.getText())){
                                     Session.setVisibility("1");
                                     Log.d("TTTSS","是--");
                                 }else{
                                     Session.setVisibility("0");
                                     Log.d("TTTSS","否--");
                                 }
            }
        });

        radioCameraEnable = (RadioButton) findViewById(R.id.radioCameraEnable);
        radioCameraDisable = (RadioButton) findViewById(R.id.radioCameraDisable);
        if("1".equals(Session.getEnableCameraValue())){
            radioCameraEnable.setChecked(true);
        }else{
            radioCameraDisable.setChecked(true);
        }
        radioCameraGroup = (RadioGroup) findViewById(R.id.radioCameraGroup);
        radioCameraGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                // TODO Auto-generated method stub
                //获取变更后的选中项的ID
                int radioButtonId = radioCameraGroup.getCheckedRadioButtonId();
                //根据ID获取RadioButton的实例
                RadioButton rb = (RadioButton)ManagerActivity.this.findViewById(radioButtonId);
                //更新文本内容，以符合选中项
                if("启用".equals(rb.getText())){
                    Session.setEnableCameraValue("1");
                    Log.d("TTTSS","启用抓拍");
                }else{
                    Session.setEnableCameraValue("0");
                    Log.d("TTTSS","禁用抓拍");
                }
            }
        });
       /* btnVisibility.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Session.setVisibility("1");
            }
        });
        btnGone.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Session.setVisibility("0");
            }
        });*/
        //0x01-M1卡2.0结构，0x02-M1卡1.2结构，0x10-CPU卡1.0结构
        ClsPublic.iCardType = Integer.valueOf(Session.getCardType());
        Log.i("TAG", "ClsPublic.iCardType:" + ClsPublic.iCardType);
        if (0x01 == ClsPublic.iCardType) {
            tv_cardtype.setText("M1卡2.0结构");
        } else if (0x02 == ClsPublic.iCardType) {
            tv_cardtype.setText("M1卡1.2结构");
            //卡结构
        } else if (0x10 == ClsPublic.iCardType) {
            tv_cardtype.setText("CPU卡1.0结构");
        } else {
            tv_cardtype.setText("无类型卡");
        }

        ll_reboottime = (LinearLayout) findViewById(R.id.ll_reboottime);
        ll_reboottime.setOnClickListener(clickListener);
        tv_ReBoottime = (TextView) findViewById(R.id.tv_reboottime);
        tv_ReBoottime.setText(Session.getReboot());

        ll_residencetime = (LinearLayout) findViewById(R.id.ll_residencetime);
        ll_residencetime.setOnClickListener(clickListener);
        tv_residencetime = (TextView) findViewById(R.id.tv_residencetime);
        tv_residencetime.setText(Session.getResidenceTime() + "秒");

        ll_onepwd = (LinearLayout) findViewById(R.id.ll_onepwd);
        ll_onepwd.setOnClickListener(clickListener);

        ll_secondpwd = (LinearLayout) findViewById(R.id.ll_secondpwd);
        ll_secondpwd.setOnClickListener(clickListener);

        ll_machineno = (LinearLayout) findViewById(R.id.ll_machineno);
        ll_machineno.setOnClickListener(clickListener);

        ll_companyinfo = (LinearLayout) findViewById(R.id.ll_companyinfo);
        ll_companyinfo.setOnClickListener(clickListener);

        ll_deviceno = (LinearLayout) findViewById(R.id.ll_deviceno);
        ll_deviceno.setOnClickListener(clickListener);
        ll_appPhoto.setOnClickListener(clickListener);
        tv_machineno = (TextView) findViewById(R.id.tv_machineno);
        tv_machineno.setText(Version.GetMachineNo());

        ll_appdownurl = (LinearLayout) findViewById(R.id.ll_appdownurl);
        ll_appdownurl.setOnClickListener(clickListener);

        tv_url = (TextView) findViewById(R.id.tv_url);
        tv_url.setText(Version.GetDownUrl());




        ll_ftpserver = (LinearLayout) findViewById(R.id.ll_ftpserver);
        ll_ftpserver.setOnClickListener(clickListener);

        tv_ftpserver = (TextView) findViewById(R.id.tv_ftpserver);
        if (Session.getFtpServerFlag().equals("1")) {
            tv_ftpserver.setText("是");
        } else {
            tv_ftpserver.setText("否");
        }


        ll_telnetd = (LinearLayout) findViewById(R.id.ll_telnetd);
        ll_telnetd.setOnClickListener(clickListener);

        tv_telnetd = (TextView) findViewById(R.id.tv_telnetd);
        if (Session.getTelnetdFlag().equals("1")) {
            tv_telnetd.setText("是");
        } else {
            tv_telnetd.setText("否");
        }

        ll_advertdownurl = (LinearLayout) findViewById(R.id.ll_advertdownurl);
        ll_advertdownurl.setOnClickListener(clickListener);
        tv_adverturl = (TextView) findViewById(R.id.tv_adverturl);
        tv_adverturl.setText(Session.getDownAdvUrl());
        if (0 == ClsPublic.imyDeivceID) { ////0--表示我的手机ANDROID设备
            ll_advertdownurl.setVisibility(View.VISIBLE);
        } else {
            ll_advertdownurl.setVisibility(View.VISIBLE);//GONE);//关闭(不占空间)VISIBLE);//开启
        }


        // String value=Session.getTestValue();
        // tv_machineno.setText("0");

        ll_overtime = (LinearLayout) findViewById(R.id.ll_overtime);
        ll_overtime.setOnClickListener(clickListener);

        tv_overtime = (TextView) findViewById(R.id.tv_overtime);
        tv_overtime.setText(Version.GetOverTime() + "秒");
        tv_key=(TextView)findViewById(R.id.tv_key);
        ll_intervaltime = (LinearLayout) findViewById(R.id.ll_intervaltime);
        ll_intervaltime.setOnClickListener(clickListener);

        tv_intervaltime = (TextView) findViewById(R.id.tv_intervaltime);
        tv_intervaltime.setText(Version.GetIntervalTime() + "秒");

        tv_companyinfo = (TextView) findViewById(R.id.tv_companyinfo);

        tv_deviceno = (TextView) findViewById(R.id.tv_deviceno);
        tv_versionno = (TextView) findViewById(R.id.tv_versionno);

        // tv_deviceno.setText(Version.GetDeviceId(this));
        tv_companyinfo.setText(Session.getCompanyInfo());// ("宇川");
        tv_deviceno.setText(Session.getDeviceNo());
        tv_versionno.setText(Version.getVersionName());

        ll_localip = (LinearLayout) findViewById(R.id.ll_localip);
        ll_localip.setOnClickListener(clickListener);

        ll_serverip = (LinearLayout) findViewById(R.id.ll_serverip);
        ll_serverip.setOnClickListener(clickListener);

        tv_localip = (TextView) findViewById(R.id.tv_localip);
        tv_localnetmask = (TextView) findViewById(R.id.tv_localnetmask);
        tv_localgetway = (TextView) findViewById(R.id.tv_localgetway);

        tv_localip.setText(Version.GetLocalIp());
        tv_localnetmask.setText(Version.GetLocalNetMask());
        tv_localgetway.setText(Version.GetLocalGetWay());
        tv_comp = (TextView) findViewById(R.id.tv_comp);
        tv_comp.setText( "M"+Session.getCom());
        tv_serverip = (TextView) findViewById(R.id.tv_serverip);
        ll_key = (LinearLayout) findViewById(R.id.ll_key);
        ll_key.setOnClickListener(clickListener);
        tv_section=(TextView)findViewById(R.id.tv_section);
        tv_serverport=(TextView)findViewById(R.id.tv_serverport);
        tv_serverip.setText(Version.GetSeverIp());
        tv_serverport.setText(Version.GetSeverPort());

        //日志服务器
        ll_serverlogip = (LinearLayout) findViewById(R.id.ll_serverlogip);
        ll_serverlogip.setOnClickListener(clickListener);
        tv_serverlogip = (TextView) findViewById(R.id.tv_serverlogip);
        tv_serverlogport = (TextView) findViewById(R.id.tv_serverlogport);
        ll_serverlogip = (LinearLayout) findViewById(R.id.ll_serverlogip);
        tv_serverlogip.setText(Version.GetSeverLogIp());
        tv_serverlogport.setText(Version.GetSeverLogPort());
        (MyApplication.getApp()).setgVisibleLogWin(false);
        if ((MyApplication.getApp()).getgVisibleLogWin()) {
            ll_serverlogip.setVisibility(View.VISIBLE);//View.GONE
        } else {
            ll_serverlogip.setVisibility(View.GONE);
        }


        ll_synchronizationtime = (LinearLayout) findViewById(R.id.ll_synchronizationtime);

        ll_synchronizationtime.setOnClickListener(clickListener);


        tv_synchronizationtime = (TextView) findViewById(R.id.tv_synchronizationtime);
        tv_synchronizationtime.setText(Session.getSynchronizationTime() + "秒");
        ll_cache = (LinearLayout) findViewById(R.id.ll_cache);
        ll_cache.setOnClickListener(clickListener);
        tv_cache = (TextView) findViewById(R.id.tv_cache);

        showCacheSize();


        setControlView(R.id.llSystemsetting);
        tvTitle.setText(rbFirst.getText());
        tv_key.setText(Session.getCarKey());
        tv_section.setText(Session.getCarSection());
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.changedeviceno.receiver");
        filter.addAction("com.changedeviceno.updater");
        filter.addAction("com.changedeviceno.Advert");
        registerReceiver(changedevicenoReceiver, filter);


    }

    private void showCacheSize() {
        try {
            tv_cache.setText(DataCleanManager.getCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void gotoSystemNetSettings() {
        Intent intent = new Intent(Settings.ACTION_SETTINGS);
        startActivity(intent);
    }

    public OnClickListener clickListener = new OnClickListener() {

        public void onClick(View v) {
            if (v.getId() == R.id.llHeaderLeft) {
                finish();
                // overridePendingTransition(R.anim.push_right_in,
                // R.anim.push_right_out);

                // overridePendingTransition(R.anim.push_left_in,
                // R.anim.push_left_out);

            } else if (v.getId() == R.id.rbFirst) {
                setControlView(R.id.llSystemsetting);
                tvTitle.setText(rbFirst.getText());
            } else if (v.getId() == R.id.rbSecond) {

                setControlView(R.id.llNetsetting);
                tvTitle.setText(rbSecond.getText());
            } else if (v.getId() == R.id.rbThree) {

                setControlView(R.id.lldeviceinfo);
                tvTitle.setText(rbThree.getText());
            } else if (v.getId() == R.id.ll_cardtype) {
                //设置卡类型
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 1068;
                } else {
                    iw = 668;
                }
                SelfDefineDialog.PopupSetCardTypeDialog(myContext,
                        R.layout.dialog_settypecard, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, tv_cardtype);
            } else if (v.getId() == R.id.ll_reboottime) {
                //设备重启时间
                String strTemp = Session.getReboot();
                int hourOfDay = Integer.valueOf(strTemp.substring(0, 2));//23;
                int minute = Integer.valueOf(strTemp.substring(3, 5));//12;
                /**
                 * 实例化一个TimePickerDialog的对象
                 * 第二个参数是一个TimePickerDialog.OnTimeSetListener匿名内部类，当用户选择好时间后点击done会调用里面的onTimeset方法
                 */
                TimePickerDialog timePickerDialog = new TimePickerDialog(ManagerActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        String strTime = "";
                        if (hourOfDay < 10) {
                            strTime += "0" + hourOfDay;
                        } else {
                            strTime += "" + hourOfDay;
                        }
                        strTime += ":";
                        if (minute < 10) {
                            strTime += "0" + minute;
                        } else {
                            strTime += "" + minute;
                        }
                        //hourOfDay + ":" + minute;
                        tv_ReBoottime.setText(strTime);
                        Session.setReboot(strTime);

//                        String shutdownTime = "1,0,0," + hourOfDay + "," + minute;
//                        Intent shutdownintent = new Intent("com.example.jt.shutdowntime");
//                        shutdownintent.putExtra("message", shutdownTime);
//                        MyApplication.getAppContext().sendBroadcast(shutdownintent);
//
//                        String powerOnTime = "1,0,0," + hourOfDay + "," + (minute + 5);
//                        Intent powerOnintent = new Intent("com.example.jt.boottime");
//                        powerOnintent.putExtra("message", powerOnTime);
//                        MyApplication.getAppContext().sendBroadcast(powerOnintent);
                    }
                }, hourOfDay, minute, true);


                timePickerDialog.show();

            } else if (v.getId() == R.id.ll_residencetime) {
                //界面停留时间
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 818;
                } else {
                    iw = 418;
                }
                SelfDefineDialog.PopupChangeMachineNoDialog(myContext,
                        R.layout.dialog_setresidencetime, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, tv_residencetime,
                        "4");

            } else if (v.getId() == R.id.ll_onepwd) {
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 868;
                } else {
                    iw = 318;
                }
                SelfDefineDialog.PopupChangePwdDialog(myContext,
                        R.layout.dialog_changepwd, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, "1");
            } else if (v.getId() == R.id.ll_secondpwd) {
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 968;
                } else {
                    iw = 318;
                }
                SelfDefineDialog.PopupChangePwdDialog(myContext,
                        R.layout.dialog_changepwd, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, "2");
            } else if (v.getId() == R.id.ll_companyinfo) {
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 968;
                } else {
                    iw = 618;
                }
                SelfDefineDialog.PopupCompanyInfoDialog(myContext,
                        R.layout.dialog_setcompanyinfo, manager, new int[]{
                                iw, LayoutParams.WRAP_CONTENT},
                        tv_companyinfo, "1");
            } else if (v.getId() == R.id.ll_machineno) {//设置机器号码
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 818;
                } else {
                    iw = 418;
                }
                SelfDefineDialog.PopupChangeMachineNoDialog(myContext,
                        R.layout.dialog_setmachineno, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, tv_machineno, "1");
            } else if (v.getId() == R.id.ll_overtime) {//设置超时时间
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 818;
                } else {
                    iw = 418;
                }
                SelfDefineDialog.PopupChangeMachineNoDialog(myContext,
                        R.layout.dialog_setovertime, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, tv_overtime, "2");
            } else if (v.getId() == R.id.ll_intervaltime) {//设置间隔时间
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 818;
                } else {
                    iw = 418;
                }
                SelfDefineDialog.PopupChangeMachineNoDialog(myContext,
                        R.layout.dialog_setovertime, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, tv_intervaltime,
                        "3");
            } else if (v.getId() == R.id.ll_synchronizationtime) {//设置同步校对时间
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 818;
                } else {
                    iw = 418;
                }
                SelfDefineDialog.PopupChangeMachineNoDialog(myContext,
                        R.layout.dialog_setsynchronizationtime, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, tv_synchronizationtime,
                        "5");
            } else if (v.getId() ==R.id. ll_localip) {
                gotoSystemNetSettings();
            } else if (v.getId() == R.id.ll_serverip) {
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 968;
                } else {
                    iw = 518;
                }
                SelfDefineDialog.PopupSetServerIpDialog(myContext,
                        R.layout.dialog_setserverip, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, new Object[]{
                                tv_serverip, tv_serverport});
            } else if (v.getId() == R.id.ll_deviceno)// 弹生产设备系列号
            {


                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 858;
                } else {
                    iw = 558;
                }
                SelfDefineDialog.PopupSetDeviceNoDialog(myContext,
                        R.layout.dialog_setdeviceno, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT});
            } else if (v.getId() == R.id.ll_appdownurl) // 在线升级
            {
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 868;
                } else {
                    iw = 668;
                }
                SelfDefineDialog.PopupSetDownAppUrlDialog(myContext,
                        R.layout.dialog_setdownurl, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, tv_url);
            } else if (v.getId() == R.id.ll_advertdownurl) //广告下载播放  // 改作图片服务设置 2015-12-15
            {
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 868;
                } else {
                    iw = 668;
                }
                SelfDefineDialog.PopupSetDownAdvertUrlDialog(myContext,
                        R.layout.dialog_setpicserver, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, tv_adverturl);

            }else  if (v.getId()==R.id.ll_appPhoto){
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 868;
                } else {
                    iw = 668;
                }
                //图片
                SelfDefineDialog.PopupSetDownPhotoUrlDialog(myContext,
                        R.layout.dialog_setpicserver, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, tv_photo);
            }
            else if (v.getId() == R.id.ll_serverlogip) {
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 968;
                } else {
                    iw = 518;
                }
                SelfDefineDialog.PopupSetServerLogIpDialog(myContext,
                        R.layout.dialog_setserverlogip, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, new Object[]{
                                tv_serverlogip, tv_serverlogport});
            } else if (v.getId() == R.id.ll_cache) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(ManagerActivity.this);
                dialog.setTitle("确认");
                dialog.setMessage("确定要清除所有的人员相片和刷卡数据?");
                dialog.setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (MyApplication.singleThreadExecutor != null)
                            MyApplication.singleThreadExecutor.execute(new DeleteCache());
                    }
                });
                dialog.setNegativeButton("否", null);
                dialog.show();
            } else if (v.getId() ==R.id. ll_com) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(ManagerActivity.this);
                dialog.setTitle("确认");
                dialog.setMessage("请选择读卡模块");
                dialog.setPositiveButton("COM3", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Session.setCom("3");
                        tv_comp.setText( "COM3");
                    }
                });
                dialog.setNegativeButton("COM4", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Session.setCom("4");
                        tv_comp.setText( "COM4");
                    }
                });
                dialog.show();
            }else if (v.getId()==R.id.ll_key){
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 818;
                } else {
                    iw = 418;
                }
                SelfDefineDialog.PopupChangeMachineNoDialog(myContext,
                        R.layout.dialog_set_key, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, tv_key, "6");

            }else  if (v.getId()==R.id.ll_section){
                int iw = 0;
                final MyApplication theApp = (MyApplication) getApplication();// 全局变量对象
                if (theApp.getSimulater()) {
                    iw = 818;
                } else {
                    iw = 418;
                }
                SelfDefineDialog.PopupChangeMachineNoDialog(myContext,
                        R.layout.dialog_setmachineno, manager, new int[]{iw,
                                LayoutParams.WRAP_CONTENT}, tv_section, "7");
            }

        }

    };


    private class DeleteCache implements Runnable {

        @Override
        public void run() {
            try {
                Glide.get(ManagerActivity.this).clearDiskCache();
                DataCleanManager.cleanDatabases(ManagerActivity.this);
                //LocalCardRecordDao localCardRecordDao = new LocalCardRecordDao();
                //localCardRecordDao.deleteAllRecord();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        com.attendance.sz.utils.Session.setAutoUploadNum("0");
                        Glide.get(ManagerActivity.this).clearMemory();
                        CrashReport.initCrashReport(ManagerActivity.this, "900012929", false);
                        DBConstants.dbHandler = null;
                        showCacheSize();
                    }
                });

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void setControlView(int resId) {
        for (int i = 0; i < llContent.getChildCount(); i++) {
            View view = llContent.getChildAt(i);

            if (view.getId() == resId) {
                view.setVisibility(View.VISIBLE);
            } else {
                view.setVisibility(View.GONE);
            }

        }
    }

    public class ProgressBarAsyncTask extends AsyncTask<String, Void, Void> {

        public String sIp;
        public String sNetMask;
        public String sGetWay;

        public ProgressBarAsyncTask() {

        }

        /**
         * 这里的Integer参数对应AsyncTask中的第一个参数 这里的String返回值对应AsyncTask的第三个参数
         * 该方法并不运行在UI线程当中，主要用于异步操作，所有在该方法中不能对UI当中的空间进行设置和修改
         * 但是可以调用publishProgress方法触发onProgressUpdate对UI进行操作
         */
        @Override
        protected Void doInBackground(String... params) {

            sIp = params[0];
            sNetMask = params[1];
            sGetWay = params[2];
            boolean isSuccess = ClsPublic.setIpWithTfiStaticIp(
                    MyApplication.getApp(), sIp, null, null);

            wifiManager.setWifiEnabled(false);

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            wifiManager.setWifiEnabled(true);
            // String ipaddress=ClsPublic.getIp(this);
            // ipaddress=ClsPublic.getLocalIpAddress();
            return null;
        }

        /**
         * 这里的String参数对应AsyncTask中的第三个参数（也就是接收doInBackground的返回值）
         * 在doInBackground方法执行结束之后在运行，并且运行在UI线程当中 可以对UI空间进行设置
         */
        @Override
        protected void onPostExecute(Void result) {

            SelfDefineLoadingDialog.CloseCurrentAlertDialog();

            Session.setLocalIp(sIp);
            Session.setLocalNetMask(sNetMask);
            Session.setLocalGetWay(sGetWay);

        }

        // 该方法运行在UI线程当中,并且运行在UI线程当中 可以对UI空间进行设置
        @Override
        protected void onPreExecute() {

        }

        /**
         * 这里的Intege参数对应AsyncTask中的第二个参数
         * 在doInBackground方法当中，，每次调用publishProgress方法都会触发onProgressUpdate执行
         * onProgressUpdate是在UI线程中执行，所有可以对UI空间进行操作
         */
        @Override
        protected void onProgressUpdate(Void... values) {

        }

    }

    public BroadcastReceiver changedevicenoReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.e("action", action);

            // Toast.makeText(ManagerActivity.this,"22111",
            // Toast.LENGTH_SHORT).show();
            if (action.equals("com.changedeviceno.receiver")) {

                SelfDefineLoadingDialog.CloseCurrentAlertDialog();
                tv_deviceno.setText(Session.getDeviceNo());



                // 倒计时
                MyModelDialog dlg = new MyModelDialog(myActivity, R.style.CustomDialogStyle, true);
                if (dlg.showDialog() == DialogResult.OK) {
                    // 或是手动点确认
                    String cmd = "su -c reboot";

                        logMa.d(LOGTAG, ":" + "getRuntime reboot before");
                        // 恢复出厂设置
                        Session.setMachineNo("0");
                        Session.setCanReadCard("0");
                        Session.setCanInit("0");
                        Session.setFirstRegister("0");
                        logMa.d(LOGTAG, ":" + "getRuntime reboot before1");

                        logMa.d(LOGTAG, ":" + "getRuntime reboot before2");


                }// end of if(dlg.showDialog() == DialogResult.OK){

            } else if (action.equals("com.changedeviceno.updater")) {
                // 进行升级检测
                Handler handler = ((MyApplication) getApplication())
                        .getHandler();
                Message message = new Message();
                message.what = 3;
                message.obj = myContext;
                handler.sendMessage(message);// updater.setUpdaterContext();updater.update();

            } else if (action.equals("com.changedeviceno.Advert")) {
                // 进行广告检测检测
                logMa.dt(LOGTAG, ":" + "advert down...");
                Handler handler = ((MyApplication) getApplication())
                        .getHandler();
                Message message = new Message();
                message.what = 5;
                message.obj = myContext;
                handler.sendMessage(message);
                logMa.dt(LOGTAG, ":" + "advert down...");
                activityOne.finish();
            }

        }
    };

    @Override
    public void onDestroy() {
        // Log.d(LOGTAG, "onDestroy()...");
        if (0 == ClsPublic.imyDeivceID) { ////0--表示我的手机ANDROID设备

        } else {
            //hide bar

        }


        super.onDestroy();
        this.unregisterReceiver(changedevicenoReceiver);
        activityOne = null;
    }
    private ArrayList<Record> loadRecordMessage() {
        mRecordList.clear();
        Cursor cursor = null;
        try {
            cursor = localCardRecordDao.GetCardRecordCursonExAdd( );
            if (cursor != null) {
                for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {

                    if (StringUtils.isNullOrEmpty(cursor.getString(cursor.getColumnIndex("card_no")))) {
                        localCardRecordDao.UpdateCardRecordFlag(cursor.getString(cursor.getColumnIndex("id")));
                        break;
                    }

                    Record record = new Record();
                    record.id = cursor.getString(cursor.getColumnIndex("id"));
                    record.recordNo = cursor.getString(cursor.getColumnIndex("card_no"));
                    record.time = cursor.getString(cursor.getColumnIndex("create_time"));
                    record.address = cursor.getString(cursor.getColumnIndex("device_no"));
                    Log.d("TAG", "create_time:" + cursor.getString(cursor.getColumnIndex("create_time")));
                    mRecordList.add(record);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return mRecordList;
    }
}
